/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.Synchronized;
import me.zhengjie.annotation.Log;
import me.zhengjie.dao.*;
import me.zhengjie.server.OutboundOrderService;
import me.zhengjie.utils.PageResult;

import me.zhengjie.vo.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.List;

/**
* @author hws
* @date 2025-06-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "出库管理")
@RequestMapping("/api/outBoundOrder")
public class OutBoundOrderController {

    private final OutboundOrderService outBoundOrderService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void exportOutBoundOrder(HttpServletResponse response, OutboundOrder criteria) throws IOException {
//        outBoundOrderService.download(outBoundOrderService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询出库单列表")
    @ApiOperation("查询outBound")
    public ResponseEntity<PageResult<OutboundOrder>> queryOutBoundOrder(OutboundOrder criteria){
        return new ResponseEntity<>(outBoundOrderService.queryAll(criteria),HttpStatus.OK);
    }



    @PostMapping
    @Log("新增outBound")
    @ApiOperation("新增outBound")
    @Transactional(rollbackFor = Exception.class)
    @Synchronized
    public Result<Integer> createOutBoundOrder(@RequestBody OutboundOrder resources) throws InterruptedException {
        int saveall = outBoundOrderService.addOutboundOrder(resources);
        return Result.success(saveall);
    }


    @PutMapping
    @Log("修改outBound")
    @ApiOperation("修改outBound")
    public ResponseEntity<Object> updateOutBoundOrder(@RequestBody OutboundOrder resources){
        outBoundOrderService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping(value = "del")
    @Log("取消outBound")
    @ApiOperation("取消outBound")
    public Result<Object> deleteOutBoundOrder(@RequestBody OutboundOrder resources) {
        resources.setStatus(10);
        return Result.success(outBoundOrderService.updateForStatus(resources));
    }

    @GetMapping("/start")
    @Log("开始出库outboundOrder")
    @ApiOperation("开始出库outboundOrder")
    public ResponseEntity<Object> startOutBoundTask(String outboundOrderNo) throws InterruptedException, UnknownHostException {
        OutboundOrderDao outboundOrder = outBoundOrderService.outboundJob(outboundOrderNo);
        return new ResponseEntity<>(outboundOrder,HttpStatus.OK);
    }

    @PostMapping("/end")
    @Log("出库单完成")
    @ApiOperation("出库单完成")
    @Transactional(rollbackFor = Exception.class)
    @Synchronized
    public Result sucessOutBoundOrder(String outboundOrderNo) {
        OutboundOrder outboundOrder = outBoundOrderService.getOne(new LambdaQueryWrapper<OutboundOrder>().eq(OutboundOrder::getOutboundOrderNo, outboundOrderNo));
        outboundOrder.setStatus(11);
        return Result.success(outBoundOrderService.updateById(outboundOrder));
    }

    @GetMapping("/compute")
    @Log("计算1F与2F的重量")
    @ApiOperation("计算1F与2F的重量")
    public ResponseEntity<Object> computeWeight(List<TransDao> transDaos) {
        ComputeDao computeDao =outBoundOrderService.computeWeight(transDaos);
        return new ResponseEntity<>(computeDao,HttpStatus.OK);
    }

    @GetMapping("/getOutOrderHistory")
    @Log("查看历史出库单")
    @ApiOperation("查看历史出库单")
    public Result<PageResult<OutboundOrderHistory>> getOutOrderHistory(OutboundOrderHistory param) {
        PageResult<OutboundOrderHistory> outOrderHistory = outBoundOrderService.getOutOrderHistory(param);
        return Result.success(outOrderHistory);
    }

    @GetMapping("/getOutOrderHistoryDetail")
    @Log("查看历史出库单详情")
    @ApiOperation("查看历史出库单详情")
    public Result<PageResult<OutboundOrderDetailDao>> getOutOrderHistoryDetail(@RequestBody OutOrderHistoryDetailDao dao) {
        PageResult<OutboundOrderDetailDao> outOrderHistory = outBoundOrderService.getOutOrderHistoryDetail(dao);
        return Result.success(outOrderHistory);
    }

    @GetMapping("/reissue")
    @Log("补发物料")
    @ApiOperation("补发物料")
    public Result<Object> reissue(@RequestBody OutboundOrderDao dao) {
//        return Result.ok(outBoundOrderService.reissue(dao));
        return Result.success(null);
    }



    @PostMapping("/noMaterials")
    @Log("非原料出库建单")
    @ApiOperation("非原料出库建单")
    public Result<PageResult<MaterialDao>> noMaterials(@RequestBody NoMaterialsDao param )  {
        return Result.success(outBoundOrderService.noMaterials(param));
    }


    @PostMapping("/handiWorkDao")
    @Log("手工建单")
    @ApiOperation("手工建单")
    public Result handiwork(@RequestBody HandiWorkDao handiWorkDao)  {
        if(outBoundOrderService.handiwork(handiWorkDao)>0){
            return Result.success();
        }else {
            return Result.error();
        }
    }
    @Transactional
    @PostMapping("/notOutBoundOrder")
    @Log("非原料建单")
    @ApiOperation("非原料建单")
    public Result notOutBoundOrder(@RequestBody OutboundOrder resources)  {
        if(outBoundOrderService.notOutBoundOrder(resources)>0){
            return Result.success();
        }else {
            return Result.error();
        }
    }










}